package com.tim.test.leetcode.algorithm;

import org.junit.Test;

import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class Distinct {

    public static void main(String[] args) {
        List<Integer> list = new Random().ints(10, -100, 100)
                .boxed().collect(Collectors.toList());
        int min = list.stream().min(Comparator.naturalOrder()).get();
        int max = list.stream().max(Comparator.naturalOrder()).get();
        int base = max-min+1;
        int mod = (int)1E9+7;// 固定大质数
        System.out.println(rabinKark(list, min, base, mod));
        System.out.println(rabinKarkASCII("askjdg"));
    }

    // 映射为n位base进制数，对mod取余，得到特征值
    // 非数值列表，转为数值列表计算
    // 特征值不同，则序列不同；相同，需逐个元素判定
    public static int rabinKark(List<Integer> list, int min, int base, int mod){
        int hash = 0;
        for(Integer item : list){
            item = item - min;// 转为正数
            hash = (hash*base+item)%mod;
        }
        return hash;
    }

    public static int rabinKarkASCII(String s){
        int hash = 0;
        int base = 128;// ascii字符的int值[0, 128)
        int mod = (int)1E9+7;
        for(char item : s.toCharArray()){
            hash = (hash*base+item)%mod;
        }
        return hash;
    }

    @Test
    public void test(){
        System.out.println((int)1e9+7);
        System.out.println((int)'0');
        System.out.println((int)'9');
        System.out.println((int)'a');
        System.out.println((int)'z');
        System.out.println((int)'A');
        System.out.println((int)'Z');
        System.out.println('A'>'a');

    }
}
